using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Configuration;

namespace YB.EnterpriseLibrary.XSLTManager
{
    public class XSLTFactory
    {
        private static XSLTManagerFactory factory = new XSLTManagerFactory(ConfigurationSourceFactory.Create());
		private static object lockObject = new object();

        public static XSLTManager GetXSLTManager()
		{
			try
			{
				lock (lockObject)
				{
					return factory.CreateDefault();
				}
			}
			catch (ConfigurationErrorsException configurationException)
			{
				TryLogConfigurationError(configurationException, "default");
				throw;
			}
		}

        public static XSLTManager GetXSLTManager(string xsltManagerName)
		{
			try
			{
				lock (lockObject)
				{
                    return factory.Create(xsltManagerName);
				}
			}
			catch (ConfigurationErrorsException configurationException)
			{
                TryLogConfigurationError(configurationException, xsltManagerName);

				throw;
			}
		}

		private static void TryLogConfigurationError(ConfigurationErrorsException configurationException, string instanceName)
		{
            //try
            //{
            //    DefaultXSLTEventLogger eventLogger = EnterpriseLibraryFactory.BuildUp<DefaultXSLTEventLogger>();
            //    if (eventLogger != null)
            //    {
            //        eventLogger.LogConfigurationError(instanceName, configurationException);
            //    }
            //}
            //catch { }
		}
	}
}
